#include "drawwidget.h"
#include <QtGui>
#include <QPen>
#include <QDebug>

DrawWidget::DrawWidget(QWidget *parent) : QWidget(parent)
{
    setAutoFillBackground(true);    //对窗体背景色的设置
    setPalette(QPalette(Qt::white));

    pix = new QPixmap(size()); // 此QPixmap对象用来准备随时接收绘制的内容（双缓冲机制）
    pix->fill(Qt::white); // 用白色填充pixmap

    pix2 = new QPixmap(*pix); // 反复resize的时候不失真

    setMinimumSize(600, 400);       //设置绘制区窗体的最小尺寸
}

void DrawWidget::setStyle(int s)
{
    style = s;
}

void DrawWidget::setWidth(int w)
{
    weight = w;
}

void DrawWidget::setColor(QColor c)
{
    color = c;
}

void DrawWidget::mousePressEvent(QMouseEvent *e)
{
    qDebug() << "mouse pressed";
    startPos = e->pos();
}

void DrawWidget::mouseMoveEvent(QMouseEvent *e)
{
    qDebug() << "mouse move";

    QPainter painter;

    QPen pen;
    pen.setStyle((Qt::PenStyle)style);
    pen.setWidth(weight);
    pen.setColor(color);

//    *pix = *pix2; // 取到原始图形

    // 在鼠标移动中，完成向pixmap中绘图的工作
    painter.begin(pix); // 指定pixmap为QPaintDevice
    painter.setPen(pen); // 设置painter的pen，还可以设置brush。画笔用于绘制轮廓，画刷用于填充
    painter.drawLine(startPos, e->pos());
    painter.end();

    // 更新pix2
    delete pix2;
    pix2 = new QPixmap(size());
    pix2->fill(Qt::white);
    QPainter p(pix2);
    p.drawPixmap(QPoint(0, 0), *pix);

    startPos = e->pos();
    update(); // 调用update会触发paint event
}

// 鼠标滚轮事件，滚轮每滚动1°，相当于移动8°，常见的滚轮鼠标波动一下滚动的角度为15°，相当于移动了15*8=120°
void DrawWidget::wheelEvent(QWheelEvent *event)
{
    const double diff = 0.1;
    int width = pix->width();
    int height = pix->height();

    qDebug() << width << height << event->pixelDelta() << event->angleDelta();

    if (event->angleDelta().y() > 0) // >0表示鼠标滚轮向前（远离用户的方向）滚动，<=表示向后滚动
    {
        int n = qMin(width, height);
        if (n >= 2000)
        {
            return;
        }

        width = int(width + width * diff);
        height = int(height + height * diff);
    }
    else
    {
        int n = qMax(width, height);
        if (n <= 200)
        {
            return;
        }

        width = int(width - width * diff);
        height = int(height - height * diff);
    }

    QPixmap *newPix = new QPixmap(width, height);
    newPix->fill(Qt::white);
    QPainter p(newPix);
    p.drawPixmap(QPoint((this->width() - newPix->width()) / 2,
                        (this->height() - newPix->height()) / 2),
                 pix2->scaled(newPix->size()));

    delete pix;
    pix = newPix;

    update();

//    resize(width, height);
}

void DrawWidget::paintEvent(QPaintEvent *)
{
    qDebug() << "paint";

    // 将pixmap绘制到widget
    QPainter painter(this); // 设置QPaintDevice为widget
    painter.drawPixmap(QPoint(0, 0), *pix);
}

void DrawWidget::resizeEvent(QResizeEvent *event)
{
    qDebug() << "resize";

//    if (height() > pix->height() || width() > pix->width())
//    {
//        QPixmap *newPix = new QPixmap(size());
//        newPix->fill(Qt::white);
//        QPainter p(newPix);
//        p.drawPixmap(QPoint(0, 0), *pix);

//        delete pix;
//        pix = newPix;
//    }

    QPixmap *newPix = new QPixmap(size());
    newPix->fill(Qt::white);
    QPainter p(newPix);
    p.drawPixmap(QPoint(0, 0), pix2->scaled(newPix->size()));

    delete pix;
    pix = newPix;

    QWidget::resizeEvent(event);
}

void DrawWidget::clear()
{
    QPixmap *clearPix = new QPixmap(size());
    clearPix->fill(Qt::white);
    pix = clearPix;
    delete clearPix;

    update();
}
